<?php

// $Id$

require_once($WSC->get_site_inc_dir().'modules/tangra_cms/_sys/tcms_admin_web_page_wed_ua.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/vpage/tcms_vpt_menu_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/menu/tcms_menu_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/vpage/tcms_vpage_template_dbc.class.php');


class VPT_Menu_Select_WP extends TCMS_Admin_Web_Page_WED_UA {
	public function create_view($name = 'default') {
		$ret = new TCMS_Admin_Page_View($this, $name, 'modules/tangra_cms/pages/_vpage/');

		return $ret;
	}


	public function init() {
		parent::init();

		$v = $this->create_view();
		$this->add_view($v);

		$template_event = new Web_Event_Simple_Int('template', Tangra_Parameter_Method::GET, 'template');

		$select_event = new Web_Event_Simple_Int('select', Tangra_Parameter_Method::GET, 'select');
		$select_event_comp = new Web_Event_Composite('select');
		$select_event_comp->add_wes($template_event);
		$select_event_comp->add_wes($select_event);

		$deselect_event = new Web_Event_Simple_Int('deselect', Tangra_Parameter_Method::GET, 'deselect');
		$deselect_event_comp = new Web_Event_Composite('deselect');
		$deselect_event_comp->add_wes($template_event);
		$deselect_event_comp->add_wes($deselect_event);

		$this->wed->add_wed_event_action_pair($select_event_comp, 'select_event', 10);
		$this->wed->add_wed_event_action_pair($deselect_event_comp, 'deselect_event', 20);
		$this->wed->add_wed_event_action_pair($template_event, 'template_event', 30);
	}


	protected function select_event($params) {
		$dbc = $this->get_dbc();

		$menu = new TCMS_Menu_DBC();
		$template = new TCMS_VPage_Template_DBC();
		if ($menu->load_by_id($dbc, $params['select']) && $template->load_by_id($dbc, $params['template'])) {
			$vpt_menu = new TCMS_VPT_Menu_DBC();
			$vpt_menu->set_menu($params['select']);
			$vpt_menu->set_template($params['template']);

			try {
				$dbc->start_trans();
				$vpt_menu->save($dbc);
				$dbc->complete_trans();
			} catch (Exception $e) {
				$dbc->fail_trans();
				$dbc->complete_trans();
				throw $e;
			}

			$view = $this->create_redirect_view('admin/modules/tangra_cms/vpage/vpage_template_manage.php', array('manage' => $params['template']));
		} else {
			$view = $this->template_event($params['template']);
		}

		return $view;
	}


	protected function deselect_event($params) {
		$dbc = $this->get_dbc();

		try {
			$dbc->start_trans();
			TCMS_VPT_Menu_DBC::delete($dbc,$params['deselect'] );
			$dbc->complete_trans();
		} catch (Exception $e) {
			$dbc->fail_trans();
			$dbc->complete_trans();
			throw $e;
		}

		$view = $this->create_redirect_view('admin/modules/tangra_cms/vpage/vpage_template_manage.php', array('manage' => $params['template']));

		return $view;
	}


	protected function template_event($template_id) {
		$dbc = $this->get_dbc();

		$template = new TCMS_VPage_Template_DBC();
		if ($template->load_by_id($dbc, $template_id)) {
			$this->export('template', $template);

			$menus = TCMS_Menu_DBC::select_menus_for_select($dbc, $template_id);
			$this->export('menus', $menus);

			$view = $this->get_view('default');
		} else {
			$view = $this->create_redirect_view('admin/modules/tangra_cms/index.php');
		}

		return $view;
	}


	protected function default_event() {
		throw new Tangra_Module_Exception('Unexpected parameters.');
	}
}